package com.itguigu.process.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itguigu.auth.service.SysUserService;
import com.itguigu.common.result.Result;
import com.itguigu.model.process.Process;
import com.itguigu.process.service.ProcessService;
import com.itguigu.process.service.ProcessTemplateService;
import com.itguigu.vo.process.ApprovalVo;
import com.itguigu.vo.process.ProcessFormVo;
import com.itguigu.vo.process.ProcessQueryVo;
import com.itguigu.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 *
 */
@RestController
@Slf4j
@RequestMapping(value = "/admin/process")
@Api(tags = "审批流管理")
@CrossOrigin // 解决跨域注解
public class ProcessController {

    @Resource
    private ProcessService processService;

    // @PreAuthorize("hasAuthority('bnt.process.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "processQueryVo", value = "查询对象", required = false)
            ProcessQueryVo processQueryVo) {
        log.info("{} , {} , {}" , page , limit , processQueryVo);
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> processIPage = processService.selectPage(pageParam , processQueryVo);
        return Result.success(processIPage);
    }

    /**
     * 启动流程功能
     */
    @ApiOperation(value = "启动流程")
    @PostMapping("/startUp")
    public Result start(@RequestBody ProcessFormVo processFormVo) {
        log.info("要开启的流程 数据内容为 : {}" , processFormVo);
        processService.startUp(processFormVo);
        return Result.success();
    }

    /**
     * 审批人查询代办任务
     * @return
     */
    @ApiOperation(value = "待处理")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending( @PathVariable Long page , @PathVariable Long limit) {
        log.info("审批人查询代办任务功能起始 ");
        Page<Process> processes = new Page<Process>(page, limit);
        return Result.success(processService.findPending(processes));
    }


    @ApiOperation(value = "查询审批详情")
    @GetMapping("show/{id}")
    public Result show(@PathVariable Long id) {
        log.info("查询审批详情 该审批的id为 : {}" , id);
        Map<String , Object> result = processService.showPendingInfo(id);
        return Result.success(result);
    }

    @ApiOperation(value = "审批通过|拒绝功能")
    @PostMapping("approve")
    public Result approve(@RequestBody ApprovalVo approvalVo) {
        log.info("审批通过拒绝穿的参数-> 流程id : {} , 任务id : {}  , 状态: {}" , approvalVo.getProcessId() , approvalVo.getTaskId()  ,  approvalVo.getTaskId());
        processService.approve(approvalVo);
        return Result.success();
    }


    @ApiOperation("查询已处理功能")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findHistoryProcessed (
            @PathVariable Long page ,
            @PathVariable Long limit
    ) {
        log.info("查询已处理功能");
        Page<Process> pageParam = new Page<>(page, limit);
        return Result.success(processService.findProcessed(pageParam));
    }


    @ApiOperation("已发起功能")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(
            @PathVariable Long page,
            @PathVariable Long limit
    ) {
        log.info("已发起功能实现 , 谁提交申请, 谁就是已发起 , 从oa-process表中进行查询");
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> resultPage = processService.findStarted(pageParam);
        return Result.success(resultPage);
    }
}
